<template>
    <view class="my-dialog" v-if="showDialog">
        <view class="my-mask"></view>
        <view class="my-container">
            <image class="img" mode="widthFix" src="/static/file/images/login-logo1.jpg"></image>
            <view class="title">微信授权</view>
            <view class="title-desc">源柚母婴商城需要获取您的用户信息</view>
            <!-- <view bindtap="confirm" class="btn algin" > -->
            <!-- <button class="btn algin" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> -->
            <button class="btn algin" @tap="getUserProfileInfo">
                <image style="width: 25px; height: 24px" mode="widthFix" src="/static/file/images/wechat_w.png"></image>
                <text class="v-center message">微信授权</text>
            </button>
        </view>
    </view>
</template>

<script>
var util = require('../../utils/util.js');
var api = require('../../utils/prom.js'); 
export default {
    data() {
        return {
            showDialog: false,
            canIUse: uni.canIUse('button.open-type.getUserInfo'),
            userInfo: ''
        };
    },
    /**
     * 组件的属性列表
     */
    props: {},
    /**
     * 组件的方法列表
     */
    methods: {
        show() { 
			 this.showDialog = true;
        },
        hide() { 
			 this.showDialog = false;
        },
        confirm() {
            //触发成功回调
            this.$emit('confirm');
            var page = getCurrentPages().pop(); //当前页面
            if (page == undefined || page == null) {
                return;
            }
            page.onShow();
            this.hide();
        },
        getUserProfileInfo(e) {
            var that = this;
            // 推荐使用uni.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
            // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗

            uni.getUserProfile({
                desc: '用于完善会员资料',
                // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
                success: (res) => {
                    app.globalData.userInfo = res.userInfo;
                    console.log(app.globalData.userInfo);
                    var userInfo = res.userInfo;
                    var param = {
                        Fnickname: escape(userInfo.nickName),
                        Fsex: userInfo.gender,
                        Flanguage: userInfo.language,
                        Fprovince: userInfo.province,
                        Fcity: userInfo.city,
                        Fcountry: userInfo.country,
                        Fheadimgurl: userInfo.avatarUrl,
                        Fopenid: uni.getStorageSync('Fopenid'),
                        Fnickname2: userInfo.nickName
                    };
                    api.request('SpChangeInfo?token=' + uni.getStorageSync('token'), 'post', param, function (res) {
                        console.log('login', param.Fnickname2);
                        var userInfo = uni.getStorageSync('user');
                        userInfo.Fnickname = param.Fnickname;
                        userInfo.Fheadimgurl = param.Fheadimgurl;
                        userInfo.Fnickname2 = unescape(param.Fnickname);
                        that.userInfo =  userInfo;
                        uni.setStorageSync('user', userInfo);
                        that.confirm();
                    });
                }
            });
        },
        getUserInfo: function (e) {
            var that = this;
            app.globalData.userInfo = e.detail.userInfo;
            console.log(app.globalData.userInfo);
            var userInfo = e.detail.userInfo;
            console.log(userInfo);
            var param = {
                Fnickname: escape(userInfo.nickName),
                Fsex: userInfo.gender,
                Flanguage: userInfo.language,
                Fprovince: userInfo.province,
                Fcity: userInfo.city,
                Fcountry: userInfo.country,
                Fheadimgurl: userInfo.avatarUrl,
                Fopenid: uni.getStorageSync('Fopenid'),
                Fnickname2: userInfo.nickName
            };
            api.request('SpChangeInfo?token=' + uni.getStorageSync('token'), 'post', param, function (res) {
                var user = uni.getStorageSync('user');
                console.log('login', param.Fnickname2);
                user.Fnickname = param.Fnickname;
                user.Fheadimgurl = param.Fheadimgurl;
                user.Fnickname2 = param.Fnickname2;
                uni.setStorageSync('user', user);
				that.userInfo = user; 
                that.confirm();
            });
        }
    },
    created: function () {}
};
</script>
<style>
@import './login.css';
</style>
